<template>
	<view class="login">
		<view class="welcome">
			<view class="item">您好，</view>
			<view class="item">欢迎注册/登录！</view>
		</view>
		<view class="input">
			<view class="item">
				<input class="phone" type="number" v-model="form.phone" maxlength="11" placeholder="请输入手机号" />
			</view>
			<view class="item">
				<input class="vercode" type="number" v-model="form.captcha" maxlength="6" placeholder="请输入验证码" />
				<view class="btn" :class="{disabled: time !== 60 || !$utils.valid.phone(form.phone)}" @click.stop="sendVerCode">{{ tips }}</view>
			</view>
		</view>
		<view class="custom-btn" :class="{disabled: !$utils.valid.phone(form.phone) || form.captcha === ''}" @click.stop="loginFn">登 录</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					phone: "",
					captcha: ""
				},
				timer: null,
				time: 60,
				tips: "发送验证码"
			}
		},
		methods: {
			sendVerCode(){
				if(this.time === 60 && this.timer === null && this.$utils.valid.phone(this.form.phone)){
					this.$utils.system.loading("发送中，请稍后");
					this.$apis.user.captcha({
						phone: this.form.phone
					}).then(res => {
						this.timer = setInterval(() => {
							this.time -= 1
							this.tips = this.time + "S后再发送"
							if(this.time <= 0){
								clearInterval(this.timer)
								this.timer = null
								this.time = 60
								this.tips = "发送验证码"
								return
							}
						}, 1000)
						this.$utils.system.tip("发送成功，5分钟内有效")
					}).catch(err => {
						console.log(err)
					}).finally(() => {
						setTimeout(() => {
							uni.hideLoading();
						}, 3000)
					});
				}
			},
			loginFn(){
				if(this.$utils.valid.phone(this.form.phone) && this.form.captcha){
					this.$utils.system.loading("登录中，请稍后");
					this.$apis.user.login(this.form).then(res => {
						this.$utils.system.tip("登录成功")
						uni.setStorageSync('token', res.token);
						this.$apis.user.getInfo().then(res => {
							uni.setStorageSync("userInfo", res);
						}).catch(err => {
							console.log(err)
						})
						setTimeout(() => {
							this.$back()
						}, 3000);
					}).catch(err => {
						console.log(err)
					}).finally(() => {
						setTimeout(() => {
							uni.hideLoading();
						}, 3000)
					});
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.login {
		width: 100%;
		height: 100vh;
		padding: 240rpx 50rpx 0rpx 50rpx;
		background-image: url("https://bchr-truck.oss-cn-hangzhou.aliyuncs.com/truck-info/gBdaqByyoT6o67ddbcab6c0bb0079a191675376684f7_1681802186532.webp");
		background-size: 100% 100%;
		.welcome{
			.item{
				font-size: 50rpx;
				font-weight: bold;
				color: $xzfx-theme;
				line-height: 80rpx;
			}
		}
		.input{
			margin-top: 100rpx;
			margin-bottom: 140rpx;
			.item{
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 100rpx;
				line-height: 100rpx;
				background-color: rgba(242,153,171,0.2);
				margin-top: 50rpx;
				border-radius: 50rpx;
				padding: 0 20rpx 0 40rpx;
				color: #333333;
				input{
					height: 100rpx;
					color: #333333;
					font-size: 32rpx;
				}
				.phone{
					width: calc(100% - 0rpx);
					height: 100rpx;
				}
				.vercode{
					width: calc(100% - 200rpx);
					height: 100rpx;
				}
				.btn{
					width: 200rpx;
					height: 70rpx;
					border-radius: 50rpx;
					font-size: 28rpx;
					font-weight: bold;
					color: #FFFFFF;
					background-color: $xzfx-theme;
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
		}
		.custom-btn {
			margin-top: 50rpx;
			width: 100%;
			height: 100rpx;
			border-radius: 50rpx;
			color: #fff;
			background-color: $xzfx-theme;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 32rpx;
			font-weight: bold;
		}
		.disabled{
			background-color: #cccdd0 !important;
			color: #fbf9fc !important;
		}
	}
</style>
